package com.ruiysoft.crm.controller;

import cn.taroco.common.vo.UserVO;
import com.ruiysoft.crm.model.entity.Bizachievement;
import com.ruiysoft.crm.model.entity.SysUser;
import com.ruiysoft.crm.service.BizachievementService;

import cn.taroco.common.utils.Query;
import cn.taroco.common.web.Response;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruiysoft.crm.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @author wxg
 */
@RestController
@RequestMapping("/bizachievement")
public class BizachievementController {
    private final SysUserService sysUserService;
    @Autowired
    private BizachievementService bizachievementService;

    public BizachievementController(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    /**
     * 通过ID查询信息
     *
     * @param id ID
     * @return
     */
    @GetMapping("/{id}")
    public Bizachievement get(@PathVariable Integer id) {
        return bizachievementService.getById(id);
    }

    /**
     * 分页查询
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @GetMapping("/page")
    public IPage dictPage(@RequestParam Map<String, Object> params) {
        return bizachievementService.page(new Query<>(params), new QueryWrapper<>());
    }

    /**
     * 添加
     *
     * @param bizachievement
     * @return success、false
     */
    @PostMapping
    public Response add(@RequestBody Bizachievement bizachievement, UserVO userVO) {
        if(userVO==null){
            userVO=new UserVO();
            userVO.setUsername("hanl");
        }
        SysUser sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUsername, userVO.getUsername()));
        bizachievement.setCreatedate(new Date());
        bizachievement.setModificationdate(new Date());
        bizachievement.setCreateid(sysUser.getUserId());
        bizachievement.setModificationid(sysUser.getUserId());
        return Response.success(bizachievementService.save(bizachievement));
    }

    /**
     * 删除
     *
     * @param id   ID
     * @return R
     */
    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Integer id) {
        return Response.success(bizachievementService.removeById(id));
    }

    /**
     * 修改
     *
     * @param bizachievement
     * @return success/false
     */
    @PutMapping
    public Response editDict(@RequestBody Bizachievement bizachievement, UserVO userVO) {
        SysUser sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUsername, userVO.getUsername()));
        bizachievement.setModificationdate(new Date());
        bizachievement.setModificationid(sysUser.getUserId());
        return Response.success(bizachievementService.updateById(bizachievement));
    }
}

/**
 * SELECT dept.`name`,SUM(achievement.`achievementMonth`) achievementMonth,SUM(IFNULL(policy.`commerceinsurance_fee`,0)+IFNULL(policy.`sali_fee`,0)) yiwancheng ,
 * SUM(achievement.`achievementMonth`)-SUM(IFNULL(policy.`commerceinsurance_fee`,0)+IFNULL(policy.`sali_fee`,0)) weiwancheng,
 * ROUND(SUM(IFNULL(policy.`commerceinsurance_fee`,0)+IFNULL(policy.`sali_fee`,0))/SUM(achievement.`achievementMonth`)*100,2) wanchenglv,
 * ROUND(DAY(NOW())/TIMESTAMPDIFF(DAY,NOW(),(DATE_ADD(NOW(),INTERVAL 1 MONTH)))*100,2) shijianjindubi,
 * ROUND(SUM(IFNULL(policy.`commerceinsurance_fee`,0)+IFNULL(policy.`sali_fee`,0))/COUNT(1),2) renjunchanneng
 * FROM sys_user users
 * LEFT JOIN sys_dept dept ON users.`dept_id`=dept.`dept_id`
 * LEFT JOIN bizachievement achievement ON achievement.`achievementType`=users.`achievement_type`
 * LEFT JOIN biz_policy policy ON users.`user_id`=policy.`user_id`
 *
 * WHERE users.del_flag=0
 * GROUP BY dept.`dept_id`;
 */
